<?php
require_once("../../../config.inc.php");
$isReloadable = true;
require_once("../../../app/_inc/include.php");
require_once "./_inc/funcFunction.php";

$errMsg = "";
if(sxREQ('reqtype') == "delete"){
	$errMsg .= functionDel();
}
$sSql = "SELECT f.*, fv.name AS fv_name, fv.description AS fv_des, fv.variable_type AS fv_type "
	." FROM function f "
	." LEFT JOIN function_variable fv ON fv.function = f.function"
	." WHERE f.function = '".sxRAS('func')."' "
	." ORDER BY fv.sequence ";
$objSDAO->query($sSql,'selFunc');
$row = $objSDAO->fetch('selFunc');
if (!$row) {
	die('no func '.sxREQ('func'));
}
sxRDef('lbr', $row['library']);
sxRDef('function', $row['function']);
sxRDef('description', $row['description']);
sxRDef('category', $row['category']);
sxRDef('language', $row['language']);
sxRDef('result_type', $row['result_type']);
sxRDef('upgrade_status', $row['upgrade_status']);
sxRDef('note', $row['note']);
sxRDef('script', $row['script']);
$kX=0;
while($row && $row['fv_name']!='') {
	$kX++;
	sxRDef('variable_name_'.$kX,$row['fv_name']);
	sxRDef('variable_desc_'.$kX,$row['fv_des']);
	sxRDef('variable_type_'.$kX,$row['fv_type']);
	$row = $objSDAO->fetch('selFunc');
}

$aOpz = array("kMenu"=>'pro.fun', $isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
function objFrmSubm(rt){
	document.objFrm.reqtype.value = rt;
	document.objFrm.submit();
}
function confDel(){
	if (confirm('Comfirm?')) {
		document.location.href = "functionView.php?func=<?=sxRHE('func')."&reqtype=delete&xsn=".XSN?>";
	}
}
</script>
<?php
echo(bodyHTML($aOpz));

echo getLibHeader(sxREQ('lbr'),true);

$aCmd = array("del"=>array("pag"=>"javascript:confDel();",
										"title"=>"delete",
										"text"=>"delete"
										),
					"mod"=>array("pag"=>"functionMod.php?func=".sxRHE('func')."&xsn=".XSN,
										"title"=>"change",
										"text"=>"change"
										),
					"back"=>array()
					);
$sCmd = commandsHTML($aCmd);
echo $sCmd;	

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan='4'>Function</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width='15%'>Name</td>" .
			"<td class=\"text01\" width='20%'>@".sxRHE('function')."</td>" .
			"<td class=\"labl01\" width='15%'>Description</td>" .
			"<td class=\"text01\" width='50%'>".sxRHE('description')."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Language</td>" .
			"<td class=\"text01\">");
$aX = getLanguages();
echo $aX[sxREQ('language')];
echo("</td>" .
			"<td class=\"labl01\">Category*</td>" .
			"<td class=\"text01\">".sxRHE('category')."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Result-type</td>" .
			"<td class=\"text01\">");
$aX = getFuncType();
echo $aX[sxREQ('result_type')];
echo("</td>" .
			"<td class=\"labl01\">Upgrade status</td>" .
			"<td class=\"text01\">");
$aX = getUpgradeStatusVal();
echo $aX[sxREQ('upgrade_status')];
echo "</td>" .
		"</tr>\n";
echo("</table>");
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='4'>Parameters</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width=\"1%\">&nbsp;</td>" .
			"<td class=\"labl01\" width=\"20%\">Name</td>" .
			"<td class=\"labl01\" width=\"20%\">Type</td>" .
			"<td class=\"labl01\" width=\"59%\">Description</td>" .
		"</tr>\n");
$aFuncType = getFuncType();
//dbg_("REQ=".print_r($_REQUEST,1));
foreach($_REQUEST as $nX => $vX) {
	if (substr($nX,0,14)!='variable_name_') continue;
	$kX = substr($nX,14);
	echo("<tr>" .
			"<td class=\"text01\" align='right'>&nbsp;$kX)&nbsp;</td>" .
			"<td class=\"text01\">".sxRHE('variable_name_'.$kX)."</td>" .
			"<td class=\"text01\">".sxVx($aFuncType,sxREQ('variable_type_'.$kX))."</td>" .
			"<td class=\"text01\">".sxRHE('variable_desc_'.$kX)."</td>" .
		"</tr>\n");
}
echo("</table>");
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"50%\">Script</td>" .
			"<td class=\"titl01\" width=\"50%\">Note</td>" .
"</tr>\n");
echo("<tr>" .
			"<td class=\"text01\">".getHtmlPreScript('script')."</td>" .
			"<td class=\"text01\">".getHtmlPreText('note')."</td>" .
"</tr>\n");
echo("</table>");

echo $sCmd;
echo("</form>");
echo(footerHTML());
exit();

function functionDel(){
	global $objSDAO;
	$errMsg = "";
	
	if($errMsg != ""){
		return $errMsg;
	}
	$sSQL = "DELETE FROM function " .
			" WHERE function = '".sxRAS('func')."' ";			
	$objSDAO->query($sSQL,"ins");
	$url = $_SESSION[USEROBJ]->getHistory(-1, true);
	serverRedir($url);
	return 'abnormal end';
}
